<?php

/**
 * venta actions.
 *
 * @package    toyomotor
 * @subpackage venta
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ventaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $vehiculo_ventas = Doctrine_Core::getTable('VehiculoVenta')
      ->createQuery('a')
      ->where('a.status = 1');
    
    $this->pager = new sfDoctrinePager('VehiculoVenta','6');
    $this->pager->setQuery($vehiculo_ventas);
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->vehiculo_venta = Doctrine_Core::getTable('VehiculoVenta')->find(array($request->getParameter('id_vehiculo_venta')));
    $this->forward404Unless($this->vehiculo_venta);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new VehiculoVentaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new VehiculoVentaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($vehiculo_venta = Doctrine_Core::getTable('VehiculoVenta')->find(array($request->getParameter('id_vehiculo_venta'))), sprintf('Object vehiculo_venta does not exist (%s).', $request->getParameter('id_vehiculo_venta')));
    $this->form = new VehiculoVentaForm($vehiculo_venta);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($vehiculo_venta = Doctrine_Core::getTable('VehiculoVenta')->find(array($request->getParameter('id_vehiculo_venta'))), sprintf('Object vehiculo_venta does not exist (%s).', $request->getParameter('id_vehiculo_venta')));
    $this->form = new VehiculoVentaForm($vehiculo_venta);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($vehiculo_venta = Doctrine_Core::getTable('VehiculoVenta')->find(array($request->getParameter('id_vehiculo_venta'))), sprintf('Object vehiculo_venta does not exist (%s).', $request->getParameter('id_vehiculo_venta')));
    $vehiculo_venta->delete();

    $this->redirect('venta/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $vehiculo_venta = $form->save();

      for ($i = 1; $i <= 7; $i++) :

	switch ($i) :
          case 1:
            $filename = $vehiculo_venta->getFoto1();
            break;
          case 2:
            $filename = $vehiculo_venta->getFoto2();
            break;
	  case 3:
            $filename = $vehiculo_venta->getFoto3();
            break;
	  case 4:
            $filename = $vehiculo_venta->getFoto4();
            break;
          case 5:
            $filename = $vehiculo_venta->getFoto5();
            break;
          case 6:
            $filename = $vehiculo_venta->getFoto6();
            break;
          case 7:
            $filename = $vehiculo_venta->getFoto7();
            break;
	endswitch;

	if ($filename!="")
    	{
	  // IMAGEN GRANDE
	  $img = new sfImage(sfConfig::get('sf_upload_dir').'/modelos/'.$filename);
	  $response = $this->getResponse();
	  $response->setContentType($img->getMIMEType());

	  $width = $img->getWidth();
	  $height = $img->getHeight();
	  $new_width = $width;
	  $new_height = $height;
	  while ($new_width >= 365 or $new_height >= 380)
	  {
            $new_width = $new_width * 99/100;
            $new_height = $new_height * 99/100;
	  }

	  $img->resize($new_width,$new_height);
	  $img->saveAs(sfConfig::get('sf_upload_dir').'/modelos/'.$filename);
/*
	  // IMAGEN FICHA
	  $img_ficha = new sfImage(sfConfig::get('sf_upload_dir').'/modelos/'.$filename);
	  $response = $this->getResponse();
	  $response->setContentType($img_ficha->getMIMEType());

	  $width = $img_ficha->getWidth();
	  $height = $img_ficha->getHeight();
	  $new_width = $width;
	  $new_height = $height;
	  while ($new_width >= 193 or $new_height >= 144)
	  {
            $new_width = $new_width * 99/100;
            $new_height = $new_height * 99/100;
	  }

	  $img_ficha->resize($new_width,$new_height);
	  $img_ficha->saveAs(sfConfig::get('sf_upload_dir').'/modelos/ficha/'.$filename);

	  // IMAGEN LISTADO
	  $img_listado = new sfImage(sfConfig::get('sf_upload_dir').'/modelos/'.$filename);

	  $response = $this->getResponse();
	  $response->setContentType($img_listado->getMIMEType());

	  $width = $img_listado->getWidth();
	  $height = $img_listado->getHeight();
	  $new_width = $width;
	  $new_height = $height;
	  while ($new_width >= 162 or $new_height >= 123)
	  {
            $new_width = $new_width * 99/100;
            $new_height = $new_height * 99/100;
	  }

	  $img_listado->resize($new_width,$new_height);
	  $img_listado->saveAs(sfConfig::get('sf_upload_dir').'/modelos/listado/'.$filename);

	  // IMAGEN THUMB
	  $img_thumb = new sfImage(sfConfig::get('sf_upload_dir').'/modelos/'.$filename);

	  $response = $this->getResponse();
	  $response->setContentType($img_thumb->getMIMEType());

	  $width = $img_thumb->getWidth();
	  $height = $img_thumb->getHeight();
	  $new_width = $width;
	  $new_height = $height;
	  while ($new_width >= 55 or $new_height >= 42)
	  {
            $new_width = $new_width * 99/100;
            $new_height = $new_height * 99/100;
	  }

	  $img_thumb->resize($new_width,$new_height);
	  $img_thumb->saveAs(sfConfig::get('sf_upload_dir').'/modelos/thumb/'.$filename);*/
	}
      endfor;

      $this->redirect($this->generateUrl('vehiculo_show', $vehiculo_venta));
    }
  }
}
